package com.ruoyi.service.impl;

import com.ruoyi.config.SmsUtil;
import com.ruoyi.domain.Building;
import com.ruoyi.domain.Community;
import com.ruoyi.feign.ResidentTaskFeignClient;
import com.ruoyi.domain.ResidentTask;
import com.ruoyi.domain.Task;
import com.ruoyi.feign.UserTaskFeignClient;
import com.ruoyi.mapper.TaskMapper;
import com.ruoyi.domain.Resident;
import com.ruoyi.service.ITaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 核酸任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
@Slf4j
public class TaskServiceImpl implements ITaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private ResidentTaskFeignClient residentTaskFeignClient;
    @Autowired
    private UserTaskFeignClient userTaskFeignClient;

    /**
     * 查询核酸任务
     *
     * @param id 核酸任务主键
     * @return 核酸任务
     */
    @Override
    public Task selectTaskById(Long id) {
        return taskMapper.selectTaskById(id);
    }

    /**
     * 查询核酸任务列表
     *
     * @param task 核酸任务
     * @return 核酸任务
     */
    @Override
    public List<Task> selectTaskList(Task task) {
        return taskMapper.selectTaskList(task);
    }

    /**
     * 新增核酸任务
     *
     * @param task 核酸任务
     * @return 结果
     */
    @Override
    public int insertTask(Task task) {
        // 设置状态
        task.setState(-1L);
        // 创建居民任务对象
        ResidentTask residentTask = new ResidentTask();
        Community community = new Community();
        Building building = new Building();
        Resident resident = new Resident();

        // feign接口通过社区id获取社区名
        community= userTaskFeignClient.communityGetInfo(task.getCommunityId());
        // feign接口通过楼栋id获取楼栋名
        building = userTaskFeignClient.buildingGetInfo(task.getBuildingId());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = task.getStartTime();
        String format = dateFormat.format(startTime);
        // 通过楼栋id获取该楼栋的所有住户
        List<Resident> residents = userTaskFeignClient.residentByBuildingId(task);
        // 任务数量为
        task.setNumberTasks((long) residents.size());
        // 遍历所有的住户 给每个住户都新建一个任务
        for (Resident residentTmp:residents) {
            residentTask.setTaskName(community.getName()+building.getName()+residentTmp.getName()+format+"核酸任务");
            residentTask.setState(2L);
            residentTask.setName(residentTmp.getName());
            residentTask.setStartTime(task.getStartTime());
            residentTask.setEndTime(task.getEndTime());
            residentTask.setAddress(task.getAddress());
            SmsUtil.send(residentTmp.getPhone(),"尊敬的" + residentTmp.getName()+",请于"+task.getStartTime()+"至"+task.getEndTime()+"内，在"+
            task.getAddress()+"进行核酸检测");
            log.info("短信内容为：" +residentTmp.getPhone(),"尊敬的" + residentTmp.getName()+",请于"+task.getStartTime()+"至"+task.getEndTime()+"内，在"+
                    task.getAddress()+"进行核酸检测");
            residentTaskFeignClient.add(residentTask);
        }

//        residentTask.setTaskName(community.getName()+building.getName()+startTime);
//        residentTask.setState(2L);
//        residentTask.setName(resident.getName());
//        residentTask.setStartTime(task.getStartTime());
//        residentTask.setEndTime(task.getEndTime());
//        residentTask.setAddress(task.getAddress());
//        residentTaskFeignClient.add(residentTask);

        return taskMapper.insertTask(task);
    }

    /**
     * 修改核酸任务
     *
     * @param task 核酸任务
     * @return 结果
     */
    @Override
    public int updateTask(Task task) {
        return taskMapper.updateTask(task);
    }

    /**
     * 批量删除核酸任务
     *
     * @param ids 需要删除的核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskByIds(Long[] ids) {
        return taskMapper.deleteTaskByIds(ids);
    }

    /**
     * 删除核酸任务信息
     *
     * @param id 核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskById(Long id) {
        return taskMapper.deleteTaskById(id);
    }

    @Override
    public int smsCode(Task task) {
        List<Resident> residents = userTaskFeignClient.residentByBuildingId(task);
        for (Resident residentTmp:residents) {
            SmsUtil.send(residentTmp.getPhone(),"尊敬的" + residentTmp.getName()+",请于"+task.getStartTime()+"至"+task.getEndTime()+"内，在"+
                    task.getAddress()+"进行核酸检测");
        }
        return residents.size();
    }

}
